import { createRouter, createWebHashHistory } from 'vue-router';
import { useMainStore } from '../store';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
NProgress.configure({ showSpinner: false });

const routes = [
	{
		path: '/',
		meta: { num: 0 },
		component: () => import('../views/index.vue'),
		redirect: '/home',
		children: [
			{
				path: '/home',
				name: 'HomePage',
				component: () => import('../views/home.vue'),
			},
			{
				path: '/space',
				name: 'SpacePage',
				component: () => import('../views/space.vue'),
			},
			{
				path: '/story',
				name: 'StoryPage',
				component: () => import('../views/story.vue'),
			},
		],
	},
	// 登录页面
	{
		path: '/login',
		name: 'LoginPage',
		component: () => import('../views/login.vue'),
	},
	{
		//404页面的路由
		path: '/:path(.*)',
		name: '404',
		component: () => import('../views/404.vue'),
	},
];
const router = createRouter({
	// history: createWebHistory(import.meta.env.VITE_BASE),
	history: createWebHashHistory(),
	routes,
});
router.beforeEach((to, from, next) => {
	NProgress.start();
	const store = useMainStore();
	if (!store.token && to.path != '/login') {
		next('/login');
		return;
	}
	next();
});
// 路由加载后
router.afterEach(() => {
	// const store = useMainStore();
	NProgress.done();
	// store.loadingProps = false;
});

export default router;
